// MuchoSucko.js for PSPTube
// Made By JK108

function MuchoSucko_CheckURL( url, option )
{
	if (url.match("http://www.muchosucko.com/show") == "http://www.muchosucko.com/show"){
		return 1;
	} else{
		return 0;
	}
}

/*
 Get a MuchoSucko URL
*/
function MuchoSucko_GetURL( url, option )
{
	var contents = GetContents(url);
	if(contents == null) { return null; /* failed to get page */ }
	var video_url = contents.match(/&file\=(.*?)&/);
	var video_url = video_url[1];
	return video_url;
}

function MuchoSucko_Search( keyword, start_index, length, option )
{
	var result = new Object();
        result.keyword   = keyword;	// Search
	result.VideoInfo = new Array();	// Results in sequence
	result.start     = start_index;	// Index of first result
	result.end       = 0;
	result.total     = -1;
	var perpage = 20;


	result.start = (((result.start - 1) / 10) * perpage) + 1;


	if(result.start < perpage){
	var contents = GetContents( "http://www.muchosucko.com/b/" + keyword.replace(/ /g, "-") );
	} else{
	var num = 1 + ((result.start - 1) / perpage);
	var contents = GetContents( "http://www.muchosucko.com/b/" + keyword.replace(/ /g, "-") + "/" + num );
	}
	if(contents == null) { return null; /* failed to get page */ }


	var pos = contents.indexOf("<div class=\"stats\">");
	contents = contents.substring(pos + 19, contents.length);
	pos = contents.indexOf(" things</div>");
	var total = contents.substring(0, pos).replace(/,/g, "");
	result.total = total;


	while(1){
	pos = contents.indexOf("<div class=\"image\" style=\" float: left; background-image: url(");

	if(pos != -1){
		//Get Thumbnail
		contents = contents.substring(pos + 62, contents.length);
		pos = contents.indexOf(");\">");
		var thumb_url = contents.substring(0, pos);
		//Get URL
		pos = contents.indexOf("<a href=\"http://www.muchosucko.com/show/");
		contents = contents.substring(pos + 9, contents.length);
		pos = contents.indexOf("\">");
		var url = contents.substring(0, pos);
		contents = contents.substring(pos + 2, contents.length);
		//Title
		pos = contents.indexOf("/search\">");
		contents = contents.substring(pos + 9, contents.length);
		pos = contents.indexOf("<");
		var title = ImproveText(contents.substring(0, pos - 2));
		//Description
		pos = contents.indexOf("description\">");
		contents = contents.substring(pos + 13, contents.length);
		pos = contents.indexOf("</span>");
		var description = ImproveText(contents.substring(0, pos));
		if(description == null || description == ""){description = title;}
		description = splitTo2Lines(description, 60);
		//Author
		pos = contents.indexOf("submitter\">submitted by ");
		contents = contents.substring(pos + 24, contents.length);
		pos = contents.indexOf("</div>");
		var author = contents.substring(0, pos);
		//Comments
		pos = contents.indexOf("likes\">");
		contents = contents.substring(pos + 7, contents.length);
		pos = contents.indexOf(" comments");
		var comments = parseInt(contents.substring(0, pos));

		info = new Object();
		info.ThumbnailURL = thumb_url;
		info.URL = url;
		info.Title = title;
		info.Description = description;
		info.Author = author;
		info.CommentCount = comments;
		info.SaveFilename = ToFilename(title);
		info.attr = 7;
		result.VideoInfo.push( info );
		if(result.VideoInfo.length == perpage){break;}
		if(result.start + (result.VideoInfo.length - 1) == result.total){break;}
	} else{
		break;
	}
	}


	if(result.VideoInfo.length) {
		result.end = result.start + (result.VideoInfo.length - 1);
	} else {
		result.total = 0;	// no results.
	}

	if(result.VideoInfo.length != perpage){result.total = result.end;}

	return result;
}

// Service metadata
var MuchoSucko = new Object();
MuchoSucko.Name          = "MuchoSucko";	/* Service name */
MuchoSucko.Description   = "MuchoSucko";	/* Service description */
MuchoSucko.SearchDesc    = "MuchoSucko";	/* Service search screen description */
MuchoSucko.SearchOSKMode = 1;			/* OSK mode 1: English only  2: Japanese */
MuchoSucko.CheckURL      = MuchoSucko_CheckURL;	/* CheckURL function */
MuchoSucko.GetURL        = MuchoSucko_GetURL;	/* GetURL function */
MuchoSucko.Search        = MuchoSucko_Search;	/* Search function */

// add YouTube to site list
SiteList.push( MuchoSucko );
